package com.lnsoft.cac.client.test;



import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


/**
 * @ClassName:CxfClient
 * @Description:webservice客户端：
 *                 该类提供两种不同的方式来调用webservice服务
 *              1：代理工厂方式
 *              2：动态调用webservice
 */
public class CxfClient {


    public static void main(String[] args) {
//        CxfClient.main1();
        CxfClient.main2();
//        CxfClient.main3();
    }

    /**
     * 1.代理类工厂的方式,需要拿到对方的接口地址
     */
//    public static void main1() {
//        try {
//            // 接口地址
//            String address = "http://127.0.0.1:18888/soap/user?wsdl";
//            // 代理工厂
//            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
//            // 设置代理地址
//            jaxWsProxyFactoryBean.setAddress(address);
//            // 设置接口类型
//            jaxWsProxyFactoryBean.setServiceClass(UserService.class);
//            // 创建一个代理接口实现
//            UserService us = (UserService) jaxWsProxyFactoryBean.create();
//            // 数据准备
//            String userId = "maple";
//            // 调用代理接口的方法调用并返回结果
//            String result = us.getUserName(userId);
//            System.out.println("返回结果:" + result);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 2：动态调用
     */
    public static void main2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://127.0.0.1:7001/CAG?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
//            objects = client.invoke("getUserName", "maple");

            Document xmlParams = new Document();
            Element request = new Element("request");
            xmlParams.addContent(request);
            Element commands = new Element("commands");
            request.addContent(commands);
            Element command = new Element("command");
            commands.addContent(command);
            command.setAttribute("commandid", "8E00B763-862E-602A-E053-96088D0A68BF-00001");
            command.setAttribute("objid", "06M98010125310001");
//            command.setAttribute("objid", "26M00090990000986");
            command.setAttribute("measurepointno", "1");
//            command.setAttribute("type", "GETEVENT");
//            command.setAttribute("type", "GETDATA");
            command.setAttribute("type", "GETDSTATDATA");
//            command.setAttribute("type", "GETMSTATDATA");
//            command.setAttribute("type", "GETCONFIG");
            // 召唤数据参数
            Element action1 = new Element("action");
//            action1.setAttribute("name", "SendFlow");
            action1.setAttribute("name", "STARTTIME");
            action1.setAttribute("value", "2019-08-31 00:00:00");
            Element action2 = new Element("action");
            action2.setAttribute("name", "ENDTIME");
            action2.setAttribute("value", "2019-08-31 23:50:59");

            command.addContent(action1);
            command.addContent(action2);


            XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
            String params = xmlOut.outputString(xmlParams);



            objects = client.invoke("downloadCAGCtrl", params);
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

    public static void main3() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://127.0.0.1:7001/CAG?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
//            objects = client.invoke("getUserName", "maple");

            Document xmlParams = new Document();
            Element request = new Element("request");
            xmlParams.addContent(request);
            Element commands = new Element("commands");
            request.addContent(commands);
            Element command = new Element("command");
            commands.addContent(command);
            command.setAttribute("commandid", "8E00B763-862E-602A-E053-96088D0A68BF-00001");
            command.setAttribute("objid", "06M98010125310001");
//            command.setAttribute("objid", "26M00090990000986");
            command.setAttribute("measurepointno", "0");
//            command.setAttribute("type", "GETEVENT");
//            command.setAttribute("type", "GETDATA");
//            command.setAttribute("type", "GETDSTATDATA");
//            command.setAttribute("type", "GETMSTATDATA");
//            command.setAttribute("type", "GETCONFIG");
            command.setAttribute("type", "SETCONFIG");
            // 召唤数据参数
            Element action1 = new Element("action");
            action1.setAttribute("name", "VMDID");
            action1.setAttribute("value", "06098010120005163");
//            action1.setAttribute("name", "STARTTIME");
//            action1.setAttribute("value", "2019-07-31 08:00:00");
//            Element action2 = new Element("action");
//            action2.setAttribute("name", "ENDTIME");
//            action2.setAttribute("value", "2019-07-31 23:50:59");

            command.addContent(action1);
//            command.addContent(action2);


            XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
            String params = xmlOut.outputString(xmlParams);



            objects = client.invoke("downloadCAGCtrl", params);
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}